package com.lm.elastic.controller;


import com.lm.elastic.model.Article;
import com.lm.elastic.service.ArticleService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/articles")
public class ArticleController {

    private final ArticleService service;

    public ArticleController(ArticleService service) {
        this.service = service;
    }

    @PostMapping
    public Article create(@RequestBody Article article) {
        return service.save(article);
    }

    @GetMapping
    public Iterable<Article> list() {
        return service.findAll();
    }

    @GetMapping("/search")
    public List<Article> search(@RequestParam String keyword) {
        return service.searchByTitle(keyword);
    }

    @GetMapping("/author/{author}")
    public List<Article> byAuthor(@PathVariable String author) {
        return service.findByAuthor(author);
    }
}
